package com.hafu.dao;

import com.hafu.model.Teacher;
import com.hafu.util.DbUtil;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;

// 教师数据访问对象
public class TeacherDao {
    private static Connection conn;

    public TeacherDao() {
        conn = DbUtil.getConn();
    }

    public Teacher getTeacherById(String teacherId) {
        if (teacherId == null || teacherId.trim().isEmpty()) {
            return null;
        }

        String sql = "SELECT * FROM teacher WHERE teacher_id = ?";
        Teacher teacher = null;

        try (PreparedStatement pstmt = conn.prepareStatement(sql)) {
            pstmt.setString(1, teacherId);

            try (ResultSet rs = pstmt.executeQuery()) {
                if (rs.next()) {
                    teacher = new Teacher(
                            rs.getString("teacher_id"),
                            rs.getString("teacher_name"), // 修改列名
                            rs.getString("gender"),
                            rs.getInt("age"),
                            rs.getString("title"),
                            rs.getString("department"),
                            rs.getString("phone")
                    );
                }
            }
        } catch (SQLException e) {
            e.printStackTrace();
        }

        return teacher;
    }

    public List<Teacher> getAllTeachers() {
        List<Teacher> teachers = new ArrayList<>();
        String sql = "SELECT * FROM teacher";

        try (PreparedStatement pstmt = conn.prepareStatement(sql);
             ResultSet rs = pstmt.executeQuery()) {

            while (rs.next()) {
                teachers.add(new Teacher(
                        rs.getString("teacher_id"),
                        rs.getString("teacher_name"), // 修改列名
                        rs.getString("gender"),
                        rs.getInt("age"),
                        rs.getString("title"),
                        rs.getString("department"),
                        rs.getString("phone")
                ));
            }
        } catch (SQLException e) {
            e.printStackTrace();
        }

        return teachers;
    }

    public List<Teacher> searchTeachers(String keyword) {
        List<Teacher> teachers = new ArrayList<>();
        String sql = "SELECT * FROM teacher WHERE teacher_id LIKE ? OR teacher_name LIKE ?"; // 修改列名

        try (PreparedStatement pstmt = conn.prepareStatement(sql)) {
            pstmt.setString(1, "%" + keyword + "%");
            pstmt.setString(2, "%" + keyword + "%");

            try (ResultSet rs = pstmt.executeQuery()) {
                while (rs.next()) {
                    teachers.add(new Teacher(
                            rs.getString("teacher_id"),
                            rs.getString("teacher_name"), // 修改列名
                            rs.getString("gender"),
                            rs.getInt("age"),
                            rs.getString("title"),
                            rs.getString("department"),
                            rs.getString("phone")
                    ));
                }
            }
        } catch (SQLException e) {
            e.printStackTrace();
        }

        return teachers;
    }

    public boolean deleteTeacher(String teacherId) {
        if (teacherId == null || teacherId.trim().isEmpty()) {
            return false;
        }

        String sql = "DELETE FROM teacher WHERE teacher_id = ?";

        try (PreparedStatement pstmt = conn.prepareStatement(sql)) {
            pstmt.setString(1, teacherId);
            return pstmt.executeUpdate() > 0;
        } catch (SQLException e) {
            e.printStackTrace();
            return false;
        }
    }
}